package com.lgqz.himalayafm.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.activity.ImgWebView;
import com.lgqz.himalayafm.bean.RankEntity;
import com.lgqz.himalayafm.adapter.RankAdapter;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.http.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 榜单
 * A simple {@link Fragment} subclass.
 */
public class RankingFragment extends Fragment implements Callback<RankEntity>,RankAdapter.OnChildCallback {
private List<RankEntity.Data>datas;
    private HttpService service;
    private ImageView imageView;
    private RankAdapter adapter1;
    private RankAdapter adapter2;
    private SimpleDraweeView img;
    private DrawerLayout drawerLayout;


    public RankingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_ranking,container,false);
        downLoad();//下载数据；
       img = (SimpleDraweeView) rootView.findViewById(R.id.tou_img);

        RecyclerView jiemuRecycler = (RecyclerView) rootView.findViewById(R.id.jiemu_recycler);
        RecyclerView zhuboRecycler = (RecyclerView) rootView.findViewById(R.id.zhubo_recycler);
        datas=new ArrayList<>();

        drawerLayout= (DrawerLayout) rootView.findViewById(R.id.ranking_drawerLayout);



        adapter1= new RankAdapter(getContext());
        adapter2 = new RankAdapter(getContext());
        jiemuRecycler.setAdapter(adapter1);
        zhuboRecycler.setAdapter(adapter2);
        adapter1.setCallback(this);
        adapter2.setCallback(this);
        return rootView;
    }


    private void downLoad() {
       service= new Retrofit.Builder()
               .baseUrl("http://mobile.ximalaya.com/mobile/discovery/")
               .addConverterFactory(GsonConverterFactory.create())
               .build()
               .create(HttpService.class);
        service.getBangDan("v2","rankingList","group","and-a1","android",true,true,2,"4.3.98").enqueue(this);

    }

    @Override
    public void onResponse(Call<RankEntity> call, Response<RankEntity> response) {

        adapter1.tianJia(response.body().getDatas().get(0).getList());

        adapter2.tianJia(response.body().getDatas().get(1).getList());
        img.setImageURI(response.body().getFocusImages().getList().get(0).getPic());


    }

    @Override
    public void onFailure(Call<RankEntity> call, Throwable t) {
        t.printStackTrace();

    }

    @Override
    public void onChildClick(int position, long id, RankEntity.ListEntity listEntity) {
        drawerLayout.openDrawer(Gravity.RIGHT);
    }
}
